﻿using System;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Windows.Controls;
using System.Windows.Input;
using Panuon.UI.Silver;
using Panuon.UI.Core;
using PropertyChanged;
using WaveringLand_Launcher.Helper;
using WaveringLand_Launcher.Helper.Net;
using WaveringLand_Launcher.Model;
using WaveringLand_Launcher.View;
using HeerDev.MLRExtension.Authenticator;
using System.Diagnostics;

namespace WaveringLand_Launcher.ViewModel
{
    [AddINotifyPropertyChangedInterface]
    public class SettingViewModel
    {
        public WllConfig Config { get; set; }

        public string LatestVersion { get; set; }

        public bool NeedUpdate => App.Version != LatestVersion;

        public int SelectedIndex { get; set; } = 0;

        public ICommand SaveBtnClickCommand { get; set; }
        public ICommand VersionCheckBtnClickCommand { get; set; }
        public ICommand ChangedTabCommand { get; set; }
        public ICommand AddAccountCommand { get; set; }
        public ICommand MicrosoftLoginCommand { get; set; }

        public SettingViewModel()
        {
            SaveBtnClickCommand = new RelayCommand(SaveBtnClick);
            VersionCheckBtnClickCommand = new RelayCommand(VersionCheckBtnClick);
            ChangedTabCommand = new RelayCommand(ChangedTab);
            AddAccountCommand = new RelayCommand(AddAccount);
            MicrosoftLoginCommand = new RelayCommand(MicrosoftLogin);

        }

        async void MicrosoftLogin(object sender)
        {

            try
            {
                App.ShowMessage("即将打开登入页面，请耐心等待结果弹窗再关闭设置页面");

                var refreshtoken = await MicrosoftLogger.FirstLogin();
                Debug.WriteLine(refreshtoken);
                var o = await MicrosoftLogger.Login(refreshtoken);
                Debug.WriteLine(o.RefreshAccessToken);
                (sender as WAccount).Account = o.Name;
                (sender as WAccount).Password = o.RefreshAccessToken;
                (sender as WAccount).ExtraCode = o.UUID;
                App.ShowMessage("账户信息获取成功,已自动保存配置文件!感谢使用，" + o.Name);
                SaveBtnClick(sender);
            }
            catch (Exception ex)
            {

                App.ShowMessage(ex.Message);
                return;
            }
        }


        void AddAccount(object sender)
        {
            Config.Accounts.Add(new WAccount());
        }

        void ChangedTab(object sender)
        {
            SelectedIndex = int.Parse((sender as RadioButton).Tag.ToString());
        }

        public void Loaded()
        {
            this.Config = ConfigurationHelper.GetConfig();
            this.RefreshLatestVersion(slient:true);
            //尝试获取Java位置
            if (String.IsNullOrEmpty(Config.JavaPath))
            {
                Config.JavaPath = ProjBobcat.Class.Helper.SystemInfoHelper.FindJava().ToList().First() ?? "";
            }
        }

        public void SaveBtnClick(object sender)
        {
            ConfigurationHelper.SaveConfig(this.Config);
            MessageBoxX.Show("保存完成");
        }

        public void VersionCheckBtnClick(object sender)
        {
            this.RefreshLatestVersion(false);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="slient">静默检查更新并更新</param>
        public void RefreshLatestVersion(bool slient = true)
        {
            LatestVersion = WlApiHelper.GetLauncherVersion();
            if (LatestVersion != App.Version && !slient)
            {
                App.ShowMessage("检测到有新版启动器可以更新~");
                VersionHelper.OpenLauncherDownPage();

            }
            else if (!slient)
            {
                App.ShowMessage("已经是最新版本啦");
            }
        }
    }
}